package productConfig

import (
	"context"
	v1 "devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"

	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取产品配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.ProductConfigGetPageLstReq) (res *v1.ProductConfigGetPageLstRes, err error) {
	res = &v1.ProductConfigGetPageLstRes{}
	res.PageLstRes, err = service.ProductConfig().GetPageLst(ctx, req)
	return
}

// Add 新增产品配置
func (*ControllerV1) Add(ctx context.Context, req *v1.ProductConfigAddReq) (res *v1.ProductConfigAddRes, err error) {
	config := &entity.AppProductConfig{
		ProductId:   req.ProductId,
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		CreatedAt:   gtime.Now(),
	}
	err = service.ProductConfig().Add(ctx, config)
	return
}

// Upt 更新产品配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.ProductConfigUptReq) (res *v1.ProductConfigUptRes, err error) {
	config := &entity.AppProductConfig{
		Id:          req.Id,
		ProductId:   req.ProductId,
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		UpdatedAt:   gtime.Now(),
	}
	err = service.ProductConfig().Upt(ctx, config)
	return
}

// Del 删除产品配置
func (*ControllerV1) Del(ctx context.Context, req *v1.ProductConfigDelReq) (res *v1.ProductConfigDelRes, err error) {
	err = service.ProductConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单条产品配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.ProductConfigGetOneReq) (res *v1.ProductConfigGetOneRes, err error) {
	res1, err := service.ProductConfig().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = new(v1.ProductConfigGetOneRes)
	res.Info = res1
	return
}

// GetOptions 获取产品配置选项
func (*ControllerV1) GetOptions(ctx context.Context, req *v1.ProductConfigGetOptionsReq) (res *v1.ProductConfigGetOptionsRes, err error) {
	res, err = service.ProductConfig().GetOptions(ctx, req)
	return
}

// GetGroupConfigs 获取配置组
func (*ControllerV1) GetGroupConfigs(ctx context.Context, req *v1.GetProductGroupConfigsReq) (res *v1.GetProductGroupConfigsRes, err error) {
	res, err = service.ProductConfig().GetGroupConfigs(ctx, req)
	return
}

// SetGroupConfigs 保存配置组
func (*ControllerV1) SetGroupConfigs(ctx context.Context, req *v1.SetProductGroupConfigsReq) (res *v1.SetProductGroupConfigsRes, err error) {
	res = &v1.SetProductGroupConfigsRes{}
	err = service.ProductConfig().SetGroupConfigs(ctx, req)
	return
}
